package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.driver.config.MinioProperties;
import com.atguigu.daijia.driver.service.FileService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * @author 谢忠涵7
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class FileServiceImpl implements FileService {
    @Autowired
    private MinioProperties minioProperties;
    /**
     * minio 上传接口
     */
    @Override
    public String upload(MultipartFile file) {
        try {
            // 创建一个Minio的客户端对象
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioProperties.getEndpointUrl())
                    .credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey())
                    .build();
            // 判断桶是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs
                    .builder().bucket(minioProperties.getBucketName())
                    .build());
            // 如果不存在，那么此时就创建一个新的桶
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(minioProperties.getBucketName())
                        .build());
            }
            // 设置存储对象名称
            String extFileName = Objects.requireNonNull(file.getOriginalFilename())
                                        .substring(file.getOriginalFilename()
                                        .lastIndexOf("."));
            String fileName = new SimpleDateFormat("yyyyMMdd")
                                 .format(new Date()) + "/"
                                 + UUID.randomUUID().toString()
                                 .replace("-" , "")
                                 + "." + extFileName;
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .object(fileName)
                    .build();
            minioClient.putObject(putObjectArgs) ;
            return minioProperties.getEndpointUrl() + "/"
                    + minioProperties.getBucketName() + "/"
                    + fileName;
        } catch (Exception e) {
            throw new GuiguException(ResultCodeEnum.FILE_FAIL);
        }
    }
}
